package top.malaoshi.webmvc.parameter;

import top.malaoshi.webmvc.info.MethodsArgumentInfo;
import top.malaoshi.webmvc.exception.ParameterTypeMatchValueException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 数组类型是基本数据类型的，包括：
 * short、int、long、float、double、boolean
 */
public class BaseTypeArrayResolver extends AbstractResolver implements RequestParamAndMethodArgsResolver {
    @Override
    public Object resolver(HttpServletRequest request, HttpServletResponse response , MethodsArgumentInfo mai) {

        checkNameAndValue(mai);
        String parameterName = getName(mai);
        String[] valueStrs = request.getParameterValues(parameterName);
        // 数组元素的类型
        Class type = mai.getElementType();
        // 用于报错提出信息
        int errorIndex = 0;
        // 如果Controlle中形参是 Integer类型，而且没有required为true，那么可以为null
        if(valueStrs!=null) {
            int arrLen = valueStrs.length;
            try {
                if(type == int.class){
                    int[] ret = new int[arrLen];
                    for(int i = 0;i<arrLen;i++){
                        errorIndex = i;
                        ret[i] = Integer.parseInt(valueStrs[i]);
                    }
                    checkRequired(ret,mai);
                    return ret;
                }else if(type == short.class){
                    short[] ret = new short[arrLen];
                    for(int i = 0;i<arrLen;i++){
                        errorIndex = i;
                        ret[i] = Short.parseShort(valueStrs[i]);
                    }

                    checkRequired(ret,mai);
                    return ret;
                }else if(type == long.class){
                    long[] ret = new long[arrLen];
                    for(int i = 0;i<arrLen;i++){
                        errorIndex = i;
                        ret[i] = Long.parseLong(valueStrs[i]);
                    }
                    checkRequired(ret,mai);
                    return ret;
                }else if(type == float.class){
                    float[] ret = new float[arrLen];
                    for(int i = 0;i<arrLen;i++){
                        errorIndex = i;
                        ret[i] = Float.parseFloat(valueStrs[i]);
                    }
                    checkRequired(ret,mai);
                    return ret;
                }else if(type == double.class){
                    double[] ret = new double[arrLen];
                    for(int i = 0;i<arrLen;i++){
                        errorIndex = i;
                        ret[i] = Double.parseDouble(valueStrs[i]);
                    }
                    checkRequired(ret,mai);
                    return ret;
                }else if(type == boolean.class){
                    boolean[] ret = new boolean[arrLen];
                    for(int i = 0;i<arrLen;i++){
                        errorIndex = i;
                        ret[i] = Boolean.parseBoolean(valueStrs[i]);
                    }
                    checkRequired(ret,mai);
                    return ret;
                }
            } catch (Exception e) {
                // 如果valueStr为非数字字符串
                String msg = String.format("从request取出的值是[ %s ]，但形参[ %s ]类型是[ %s数组 ]，不能赋值",
                        valueStrs[errorIndex],mai.getName(),mai.getElementType().getName());
                throw new ParameterTypeMatchValueException(msg);
            }
        }
        // 此时返回值为null，需要校验 @RequestParam 注解的 Required 配置
        checkRequired(null,mai);
        return null;
    }
}
